#!/usr/bin/env python

from DataColumn import *
from DataRow import *
from DataTable import *
from DataSchema import *
import xml.dom.minidom


class DataSet:
    def _get_name(self):
        return self.__name
    def _set_name(self, name):
        self.__name = name
    
    def _get_schemas(self):
        return self.__schemas
    def _get_tables(self):
        return self.__tables
    
    def _get_adapter(self):
        return self.__adapter
    def _set_adapter(self, adapter):
        self.__adapter = adapter
    
    name = property(_get_name, _set_name)
    schemas = property(_get_schemas)
    tables = property(_get_tables)
    adapter = property(_get_adapter, _set_adapter)
    
    def __init__(self, name = None):
        self.__schemas = dict()
        self.__tables = dict()
        self.name = name
        
    def load_file(self, file):
        xmlDom = xml.dom.minidom.parse(file)
        self.load_xml(xmlDom)
    
    def load_xml(self, xmlDom):
        if (not xmlDom.tagName == "DataSet"):
            raise Exception()
        
        for schemasNode in xmlDom.getElementsByTagName("adapter"):
            #TODO:  DO THIS!!!!
            break
        
        for schemasNode in xmlDom.getElementsByTagName("schemas"):
            for schemaNode in schemasNode.getElementsByTagName("DataSchema"):
                    ds = DataSchema(schemaNode.getAttribute("name"))
                    ds.dataset = self
                    self.schemas[ds.name] = ds
                    ds.load_xml(schemaNode)
            break
        
        
        for tablesNode in xmlDom.getElementsByTagName("tables"):
            for tableNode in tablesNode.getElementsByTagName("DataTable"):
                dt = DataTable(self.schemas[tableNode.getAttribute("schema")], tableNode.getAttribute("name"))
                self.tables[dt.name] = dt
                dt.load_xml(tableNode)
            break
        
        pass
    
    def save_file(self, filePath):
        pass
    
    def save_xml(self):
        pass
